package com.zhiyou.exception;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

public class CustomExceptionResolver implements HandlerExceptionResolver {

	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object arg2, Exception e) {
		/*
		 * 异常分为两类
		预处理的异常：你写代码的时候知道某个地方会报异常，这个异常可以自定义
		运行时异常：程序在运行过程中，不知道在某个地方会报异常

		 */

		/*输入输入流  
		 * 将错误信息输入到Writer  ,从Writer获取字符串
		 **********if中的判断是为了自己定义异常***********/
		/*String messgae ="";
		if (e instanceof MyException) {
			messgae =e.getMessage();
		} else {
			Writer out =new  StringWriter();
			PrintWriter sWriter =new PrintWriter(out);
			e.printStackTrace(sWriter);
			messgae=out.toString();
		}*/
		
		Writer out =new  StringWriter();
		PrintWriter sWriter =new PrintWriter(out);
		e.printStackTrace(sWriter);
		
		ModelAndView modelAndView =new ModelAndView();
		//在页面上展示异常信息
		modelAndView.addObject("message",out.toString());
		//modelAndView.addObject("message",messgae);
		modelAndView.setViewName("error/error");
		
		return modelAndView;
	}

}
